<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_fctrl_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/forms/attachment_upload/attachment_upload_form_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_attachments_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_composite_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class Attachment_Upload_WP extends TCMS_Admin_Page_W_FCtrl_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_attachment/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('vpage');
		$this->add_transit_var('category');
		$this->add_transit_var('ret');
		$this->add_transit_var('vpt_context');
		$this->add_transit_var('back_to_select');
	}


	protected function init_events() {
		$submit_events = $this->form_ctrl->get_form_submit_events();
		foreach($submit_events as $event_submit) {
			$this->wed->add_wed_event_action_pair($event_submit, 'form_submit');
		}

		$event_upload = new Web_Event_Simple('upload', Tangra_Parameter_Method::GET, 'upload');
		$this->wed->add_wed_event_action_pair($event_upload, 'event_upload');


	}


	protected function event_upload() {
		$this->export('vpt_context', $this->get_transit_var('vpt_context', true));
		$this->export('back_to_select', $this->get_transit_var('back_to_select', true));

		$this->on_add_new();

		$vpage_id = $this->get_transit_var('vpage', true);
		if ($vpage_id) {
			$vpage = new TCMS_VPage_DBC();
			$dbc = $this->get_dbc();
			if ($vpage->load_by_id($dbc, $vpage_id)) {
				$this->export('vpage', $vpage);
			}
		}

		return $this->show_form();
	}


	protected function form_submit($params) {
		$this->export('vpt_context', $this->get_transit_var('vpt_context', true));
		$this->export('back_to_select', $this->get_transit_var('back_to_select', true));

		$vpage_id = $this->get_transit_var('vpage', true);
		if ($vpage_id) {
			$vpage = new TCMS_VPage_DBC();
			$dbc = $this->get_dbc();
			if ($vpage->load_by_id($dbc, $vpage_id)) {
				$this->export('vpage', $vpage);
			}
		}

		return parent::form_submit($params);
	}


	protected function create_form_ctrl() {
		$dbc = $this->get_dbc();
		$tvm = $this->get_tvm();

		$settings_attachments = new TCMS_Settings_Attachments_DBC();
		if ($settings_attachments->load_by_id($dbc, 1)) {
			$ret = new Attachment_Upload_Form_Ctrl('attachment_upload', $tvm, $settings_attachments, $dbc, $this->_config->get_uploads_dir());
			$context = $this->get__context();
			if ($context->exists_in_get('ci')) {
				$ci_id = $context->get_from_get('ci');
				$ret->set_permanent_external_param('ci', $ci_id);

				if ($ci_id) {
					$ci = new TCMS_CI_Composite_DBC($this->_config->get_hidden_dir().'inc/');
					if (!$ci->load_by_id($dbc, $ci_id)) {
						throw new Tangra_Module_Exception('Cannot load ci item with id = '.$ci_id);
					}
					$this->export('ci_title', $ci->get_as_short_plain_text());
				}

				$this->export('ci_id', $ci_id);
			}
		} else {
			throw new Tangra_Module_Exception('Cannot load settings attachments');
		}

		$ret_value = $this->get_transit_var('ret', true);
		if ($ret_value) {
			$this->export('ret', urldecode($ret_value));
		}

		$vpage_id = $this->get_transit_var('vpage', true);
		$this->export('vpage_id', $vpage_id);

		return $ret;
	}

	protected function on_add_new() {
		$current_user = $this->get_user();
		$this->form_ctrl->set_permanent_external_param('user_id', $current_user->get_id());
	}


	protected function on_received_id($object_id) {
		throw new Tangra_Module_Exception('only add_new supported');
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Unexpected parameters.');
	}


	protected function create_default_redir_view() {
		$ci_id = $this->form_ctrl->get_permanent_external_param('ci', true);

		if ($ci_id) {
			$redir_view = $this->create_redirect_view('admin/modules/tangra_cms/ci/ci_attachments.php', array('ci' => $ci_id));
		} else {
			$redir_view = $this->create_redirect_view($this->get_redir_on_success());
		}

		return $redir_view;
	}
}